package me.aartikov.alligator.commands;


import me.aartikov.alligator.OhosNavigator;
import me.aartikov.alligator.NavigationContext;
import me.aartikov.alligator.Screen;
import me.aartikov.alligator.ScreenResult;
import me.aartikov.alligator.animations.AnimationData;
import me.aartikov.alligator.exceptions.NavigationException;

/**
 * Command implementation for {@code finish} method and {@code finishWithResult} method of {@link OhosNavigator}.
 */
public class FinishCommand implements Command {

    private ScreenResult mScreenResult;

    private boolean mForTopLevel;


    private AnimationData mAnimationData;
    private Screen mScreen;


    public FinishCommand(Screen screen, ScreenResult screenResult, boolean forTopLevel, AnimationData animationData) {
        mScreenResult = screenResult;
        mForTopLevel = forTopLevel;
        mAnimationData = animationData;
        mScreen = screen;
    }

    @Override
    public boolean execute(NavigationContext navigationContext) throws NavigationException {
        if (!mForTopLevel && navigationContext.getFlowFragmentNavigator() != null && navigationContext.getFlowFragmentNavigator().canGoBack()) {
            navigationContext.getFlowFragmentNavigator().goBack(mScreen, mScreenResult, mAnimationData);
            return true;
        } else {
            navigationContext.getActivityNavigator().goBack(mScreenResult, mAnimationData);
            return false;
        }
    }
}
